<?php
/*
 * @Author: Wulala
 * @Date: 2025-04-08 11:46:09
 * @LastEditTime: 2025-04-11 16:15:06
 * @LastEditors: Wulala
 * @Description: 
 * @FilePath: \exchange\app\api\service\SignService.php
 * 可以输入预定的版权声明、个性签名、空行等
 */

namespace app\api\service\delivery\dada;
use think\facade\Log;

class ApiService
{
    // 基础参数
    protected $config;//配置

    // 请求地址
    protected $openCitiesLists = 'api/cityCode/list';             //1.查询开通城市
    protected $registerMerchant = '/merchantApi/merchant/add';    //2.商户注册
    protected $queryAllStores = '/api/shop/detail';               //2.分页查询商户店铺
  
    protected $orderCalculate = '/api/order/queryDeliverFee';         //5.订单计费
    protected $orderPlace = '/api/order/addAfterQuery';               //6.提交订单
    protected $addition = '/api/order/addTip';                        //7.订单加价(加小费)
    protected $orderInfo = '/api/order/status/query';                 //8.查询订单详情
    protected string $courierInfo = '/api/order/transporter/position';//9.查询骑士位置
    protected $calculateOrderAddWeightFee = '/openapi/merchants/v5/calculateOrderAddWeightFee'; //10.查询订单续重加价金额
    protected $payAddWeightFee = '/openapi/merchants/v5/payAddWeightFee';       //11.支付订单续重费用
    protected $preAbortOrder = '/openapi/merchants/v5/preAbortOrder';           //12.订单预取消
    protected $abortOrder = '/api/order/formalCancel';        //13.订单取消
    protected string $confirmGoodsReturn = '/api/order/confirm/goods';//14.确认物品送回(商户确认物品已返还)
    protected $addStores = '/api/shop/add';                       //15.新增店铺
    protected $modifyStore = '/api/shop/update';              //16.修改店铺
    protected $getUserAccount = '/api/balance/query';         //17.查询账号额度
    protected $orderEta = '/openapi/merchants/v5/orderEta';   //21.查询订单ETA
    protected $appendOrder = '/api/order/appoint/exist';      //22.订单追单


    protected $cancelAppendOrder = '/api/order/appoint/cancel';//取消追加
    protected string $merchantExamine = '/api/message/confirm';//商户审核骑士取消订单
    protected string $transporter = '/api/order/transporter/track';//获取骑士配送信息H5页面
    protected string $fetchCode = '/api/fetchCode/update';//更新取件码和货架号
    protected string $afterSale = '/api/order/afterSale/confirm';//商户确认售后单交接完成
    protected $cancelReasons = '/api/order/cancel/reasons';//订单取消原因
    protected $complaint = '/api/complaint/dada';//商家投诉
    protected $complaintReasons = '/api/complaint/reasons';//商家投诉原因



    /*
    * $config: 配置信息
    */
    public function __construct(array $config)
    {
        $this->config = $config;
    }


    // 达达平台---统一请求接口
    public function  sendUnite($urlApi,$data=array())
    {
        $url = $this->config['url'].$this->$urlApi;

        $param['app_key'] = $this->config['app_key'];

        if (!empty($data)){
            $param['body'] = $data;
        }else{
            $param['body'] ='{}';
        }

        $param['format'] = $this->config['format'];
        $param['source_id'] =  $this->config['source_id'];
        $param['timestamp'] = time();
        $param['v'] = $this->config['version'];
        $param['signature'] = $this->signature($param,$this->config['app_secret']);
        $reqJson=json_encode($param);
        Log::info('达达-请求报文---:' . $reqJson);
        // var_dump($reqJson);exit;

        $retMsg=curl_post($url, $reqJson,null, $this->config['format']);
        Log::info('达达-请求返回值---:' . $retMsg);
        // var_dump($retMsg);exit;
      
        return  json_decode($retMsg,true);
    }

    // 达达统一返回接口
    public function  returnUnite($res=array())
    {
        if(isset($res['status'])&&$res['status']=='success'){
           return result_success('success',$res["result"]);
        }else{
           return result_error($res["msg"]??'请求失败！');
        } 
    }


    // 计算签名
    public function signature($param, $appSecret)
    {
        $signStr=null;
        //将数组按键名进行字典排序
        ksort($param);
        //将排序过后的参数，进行key和value字符串拼接
        foreach ($param as $key => $value)
            $signStr.= $key . $value;

        //将拼接后的字符串首尾加上app_secret秘钥
        $signStr= $appSecret.$signStr.$appSecret;
        //对签名字符串进行MD5加密转换为大写 并返回
        Log::info('达达-验签字符串---:' . $signStr);
        return  strtoupper(md5($signStr));
    }

}